package client;

import gui.Cart_Gui;
import client.ItemsInCustomItem;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RemoveItems extends JFrame implements ActionListener 
{
	public JPanel panel = null;
	public JLabel removeItem = null;
	public JLabel quantity = null;
	public JButton Remove = null;
	public JButton RemoveAll = null;
	public JTextField quantityToRemove = null;
	public String callFrom = null;
	int msg = 2;
	
	public RemoveItems(String call)
	{
		callFrom = call;
		this.setTitle("Remove Item");
		panel = new JPanel();
		panel.setLayout(null);
		this.setPreferredSize(new Dimension(300, 220));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setResizable(false);
		this.addWindowListener(new WindowAdapter() 
        {public void windowClosing(WindowEvent e){dispose();}});
		
		removeItem = new JLabel("Remove Item(s)");
		removeItem.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,18));
		removeItem.setBounds(20, 1, 200, 30);
  	  	panel.add(removeItem);
  	  	
  	  	quantity = new JLabel("Quantity To Remove: ");
	  	quantity.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,16));
	  	quantity.setBounds(20, 30, 200, 40);
	  	panel.add(quantity);
	  	
	  	quantityToRemove = new JTextField();
	  	quantityToRemove.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,16));
	  	quantityToRemove.setBounds(210, 30, 70, 40);
	  	panel.add(quantityToRemove);

  	  	Remove = new JButton("Remove");
  	  	Remove.setBounds(20, 80, 260, 40);
  	  	Remove.addActionListener(this);
  	  	panel.add(Remove);
  	  	
  	  	if(callFrom.equals("ItemsInCustomItem"))
  	  		RemoveAll = new JButton("Remove All   ("+(Integer)ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 4)+")  ");
  	  	else
  	  		RemoveAll = new JButton("Remove All   ("+(Integer)Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 4)+")  ");
  	  	RemoveAll.setBounds(20, 130, 260, 40);
  	  	RemoveAll.addActionListener(this);
	  	panel.add(RemoveAll);
	  	
	  	this.add(panel);
	  	this.pack();
	  	this.setEnabled(true);
	  	this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == Remove || e.getSource() == RemoveAll)
		{
			boolean flag = true;
			if(e.getSource() == Remove)
			{
				try{Integer.valueOf(quantityToRemove.getText().trim()).intValue();}
			    catch (NumberFormatException nfe)
			    {
			    	JOptionPane.showMessageDialog(null,"Only digits are permitted, Reenter quantity to remove ","Error",JOptionPane.ERROR_MESSAGE);
			    	flag = false;
			    }
			}
			if(flag == true)
			{
				if(callFrom.equals("ItemsInCustomItem"))
				{
					if(e.getSource() == Remove && 
							(quantityToRemove.getText().isEmpty() 
									|| Integer.parseInt(quantityToRemove.getText()) > (Integer)ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 4)))
					{
						JOptionPane.showMessageDialog(null,"You have "+(Integer)ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 4)+" in your custom item");
						quantityToRemove.setText((String)(ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 4)).toString());
					}
					
					msg = JOptionPane.showConfirmDialog(null, "Are you sure you want to remove this item(s)? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION);
		   		 	if(msg == 0)		//CANCEL = 2 //OK = 0	
		   		 	{	
		   		 		Object[] temp = new Object[4];
		   		 		temp[0] = (Integer)ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 0);		//item id
		   		 		temp[1] = (Integer)Cart_Gui.customItemsTable.getValueAt(Cart_Gui.customItemsTable.getSelectedRow(), 0);	//custom id
		   		 		
		   		 		if(e.getSource() == Remove)
		   		 			temp[2] = Integer.parseInt(quantityToRemove.getText()); //if set Quantity
		   		 		else 
		   		 			if(e.getSource() == RemoveAll)
		   		 				temp[2] = (Integer)ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 4);	//all Quantity		
		   		 		temp[3] = (Integer)ItemsInCustomItem.table.getValueAt(ItemsInCustomItem.table.getSelectedRow(), 4); //init Quantity
		   		 		
		   		 		if(Main.cart_Control != null)
		   		 			Main.cart_Control = null;
		   		 		Main.cart_Control = new Cart("removeItemFromCustom", temp);
		   		 		int result = (Integer)Main.clie.getResult();
		   		 		System.out.println(result);
		   		 		
		   		 		JOptionPane.showMessageDialog(null,"Item(s) removed");
		   		 		Cart_Gui.ItemsInCustom.dispose();
		   		 		dispose();
		   		 	}
				}
				else 
					if(callFrom.equals("userCart"))
					{
						if(e.getSource() == Remove 
								&& (quantityToRemove.getText().isEmpty() 
										||  Integer.parseInt(quantityToRemove.getText()) > (Integer)Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 4)))
						{
							JOptionPane.showMessageDialog(null,"You have "+(Integer)Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 4)+" in your custom item");
							quantityToRemove.setText((String)(Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 4)).toString());
						}
						
						msg = JOptionPane.showConfirmDialog(null, "Are you sure you want to remove this item(s)? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION);
			   		 	if(msg == 0)		//CANCEL = 2 //OK = 0	
			   		 	{	
			   		 		Object[] temp = new Object[3];
			   		 		temp[0] = (Integer)Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 0);	//item id
			   		 		
			   		 		if(e.getSource() == Remove)
			   		 			temp[1] = Integer.parseInt(quantityToRemove.getText());  //if set Quantity
			   		 		else 
			   		 			if(e.getSource() == RemoveAll)
			   		 				temp[1] = (Integer)Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 4); //all Quantity		
			   		 		temp[2] = (Integer)Cart_Gui.table.getValueAt(Cart_Gui.table.getSelectedRow(), 4);		  //init Quantity
			   		 		
			   		 		if(Main.cart_Control != null)
		   		 				Main.cart_Control = null;
			   		 		Main.cart_Control = new Cart("removeItemFromCart", temp);
			   		 		int result = (Integer)Main.clie.getResult();
			   		 		System.out.println(result);
			   		 		
			   		 		JOptionPane.showMessageDialog(null,"Item(s) removed");
			   		 		
			   		 		dispose();
			   		 	}
					}
				if(msg == 0)
				{
					if(Cart_Gui.callFrom.equals("MainScreen"))
						Main.main.cart.doClick();
		
					else if(Cart_Gui.callFrom.equals("Catalog"))
							Main.catalog.goToCart.doClick();		
					
					else if(Cart_Gui.callFrom.equals("CustomOrder"))
							Main.customOrder.goToCart.doClick();
				}
			}
			else quantityToRemove.setText("");
		}
	}
}
